var data = ["1", 1, 2, "2", 3, 4, "3", 1, 2, 4, "4", 3];
var transform = function(data) {
  var arr = data.reduce((acc, item) => {
    if (typeof item === "string") {
      acc.group = item;
    } else {
      acc.push([Number(acc.group), item]);
    }
    return acc;
  }, []);
  let result = [];
  let index = 1;
  function loop(arr, result, index) {
    let len = arr.length;
    if (len > 0) {
      for (var i = 0; i < len; i++) {
        if (arr[i][1] === index) {
          result.push(arr[i]);
          arr.splice(i, 1);
          if (index < 4) {
            index++;
          } else {
            index = 1;
          }
          loop(arr, result, index);
          break;
        }
      }
    }
  }
  loop(arr, result, index);
  return result;
};

console.log(transform(data));
